<?php
namespace App\Api\Controllers\Validators;

use App\Models\api\Card;
use Illuminate\Http\Request;
use Validator;

class ClockValidator
{
	/**
	 * 打卡 数据验证
	 *
	 * @param Request $request
	 * @return mixed
	 * */
	public static function clock(Request $request)
	{
//		$only = ['status'];
//
//		$rules = [
//			'status' => 'required|regex:/^[1-2]$/',
//		];
//
//		$messages = [
//
//			'status.required' => '必须选择是上班还是下班打卡',
//			'status.regex' => '必须选择是上班还是下班打卡',
//		];
//
//		$input = $request->only($only);
//
//		$validator = Validator::make($input, $rules, $messages);
//
//		if ($validator->fails())
//			exit(json_encode(['info'=>$validator->errors()->first(),'code'=>'1002']));

		$input['user_id'] = constant("_USER_ID")  ?: null;
		$input['company_id'] = constant("_USER_CORPORATION_ID") ?: null;

		if (!$input['company_id'] || !$input['user_id'])
			exit(json_encode(['info'=>'你还么有组织，打什么卡？快去加入个公司吧。','code'=>'1002']));

		return $input;
	}


    /**
     * @param Request $request
     * @return array
     * @name 打卡
     */
	public static function create(Request $request)
	{
		$only = ['company_id','user_id','description','location','longitude','latiude','type','status','image_url','is_outside'];
		$rules = [
				'location' => 'required',
				'longitude' => 'required',
				'latiude' => 'required',
		];
		$messages = [
				'location.required' => '打卡地址不能为空',
				'longitude.required' => '经度不能为空',
				'latiude.required' => '纬度不能为空',
				'status.required' => '请选择打卡。',
				'type.required' => '请选择是上班或者下班打卡。',

		];
		$input = $request->only($only);

		$validator = Validator::make($input, $rules, $messages);

		if ($validator->fails())
			exit(json_encode(['info'=>$validator->errors()->first(), 'code' => 1002]));

		return $input;

	}

}